<template>
  <div class="ma-content-block">
    <sa-table ref="crudRef" :options="options" :columns="columns" :searchForm="searchForm">
      <!-- 搜索区 tableSearch -->
      <template #tableSearch>
        <a-col :span="8">
          <a-form-item label="ID" field="id">
            <a-input v-model="searchForm.id" placeholder="请输入ID" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="模板 ID" field="template_id">
            <a-select
              v-model="searchForm.template_id"
              :field-names="{ label: 'name', value: 'id' }"
              :options="optionData.template_id"
              placeholder="请选择模板 ID"
              allow-clear
            />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="支付方式" field="type">
            <sa-select v-model="searchForm.type" dict="pay_type" placeholder="请选择支付方式" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="名称" field="name">
            <a-input v-model="searchForm.name" placeholder="请输入名称" />
          </a-form-item>
        </a-col>
      </template>

      <!-- Table 自定义渲染 -->
    </sa-table>

    <!-- 编辑表单 -->
    <edit-form ref="editRef" @success="refresh" />
  </div>
</template>

<script setup>
import { onMounted, ref, reactive } from 'vue'
import { Message } from '@arco-design/web-vue'
import commonApi from '@/api/common'
import EditForm from './edit.vue'
import api from '../api/price'

// 引用定义
const crudRef = ref()
const editRef = ref()
const optionData = reactive({
  template_id: [],
})

// 搜索表单
const searchForm = ref({
  id: '',
  template_id: '',
  type: '',
  name: '',
})


// SaTable 基础配置
const options = reactive({
  api: api.getPageList,
  recycleApi: api.getRecyclePageList,
  rowSelection: { showCheckedAll: true },
  add: {
    show: true,
    auth: ['/ai/TemplatePrice/save'],
    func: async () => {
      editRef.value?.open()
    },
  },
  edit: {
    show: true,
    auth: ['/ai/TemplatePrice/update'],
    func: async (record) => {
      editRef.value?.open('edit')
      editRef.value?.setFormData(record)
    },
  },
  delete: {
    show: true,
    auth: ['/ai/TemplatePrice/destroy'],
    func: async (params) => {
      const resp = await api.delete(params)
      if (resp.code === 200) {
        Message.success(`删除成功！`)
        crudRef.value?.refresh()
      }
    },
    realAuth: ['/ai/TemplatePrice/realDestroy'],
    realFunc: async (params) => {
      const resp = await api.realDestroy(params)
      if (resp.code === 200) {
        Message.success(`销毁成功！`)
        crudRef.value?.refresh()
      }
    },
  },
  recovery: {
    show: true,
    auth: ['/ai/TemplatePrice/recovery'],
    func: async (params) => {
      const resp = await api.recovery(params)
      if (resp.code === 200) {
        Message.success(`恢复成功！`)
        crudRef.value?.refresh()
      }
    },
  },
})

// SaTable 列配置
const columns = reactive([
  { title: 'ID', dataIndex: 'id', width: 180, sortable: { sortDirections: ['ascend', 'descend'] } },
  { title: '模板 ID', dataIndex: 'template_id', width: 180 },
  { title: '价格排序', dataIndex: 'sort', width: 180 },
  { title: '支付方式', dataIndex: 'type', dict: 'pay_type', width: 180 },
  { title: '名称', dataIndex: 'name', width: 180 },
  { title: '价格', dataIndex: 'price', width: 180 },
  { title: '创建时间', dataIndex: 'create_time', width: 180 },
  { title: '更新时间', dataIndex: 'update_time', width: 180 },
])

// 页面数据初始化
const initPage = async () => {
  const resp_template_id = await commonApi.commonGet('/ai/Template/index?saiType=all')
  optionData.template_id = resp_template_id.data
}

// SaTable 数据请求
const refresh = async () => {
  crudRef.value?.refresh()
}

// 页面加载完成执行
onMounted(async () => {
  initPage()
  refresh()
})
</script>
